﻿using DevExpress.XtraEditors.Controls;
using RecruitStudents.Client.BLL;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Json;
using RecruitStudents.Utility.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Base
{
    public partial class Base_ExamPlace_Option : BaseForm
    {
        string ExamPlaceID = string.Empty;
        ServiceBase service = new ServiceBase();
        TB_Sys_ExamPlace model = null;
        public Base_ExamPlace_Option(string strExamPlaceID)
        {
            InitializeComponent();
            ExamPlaceID = strExamPlaceID;
            BindCity();
            if (ExamPlaceID.ToSafeString().Length > 0)
            {
                model = service.GetSysExamPlaceModel(ExamPlaceID);
                if (model != null)
                {
                    txtNo.Enabled = false;
                    cmbKD.EditValue = model.ExamProvince;
                    txtNo.Text = model.ExamPlaceNO;
                    txtName.Text = model.ExamPlaceName;
                    txtAddress.Text = model.Address;
                    if (model.IsActive == "1")
                    {
                        chkIsClose.Checked = true;
                    }
                    else
                    {
                        chkIsClose.Checked = false;
                    }

                    using (DataTable dt = service.GetSysExamPlace_Limit(model.ExamPlaceNO))
                    {
                        if (dt != null)
                        {
                            int i = 0;
                            while (chkListExamProvince.GetItemValue(i) != null)
                            {
                                if (dt.Select("ExamProvince='" + chkListExamProvince.GetItemValue(i) + "'").Length > 0)
                                {
                                    chkListExamProvince.SetItemChecked(i, true);
                                }
                                else
                                {
                                    chkListExamProvince.SetItemChecked(i, false);
                                }
                                i++;
                            }
                        }
                    }
                }
            }
        }

        #region 加载考点省份
        void BindCity()
        {
            this.cmbKD.Properties.NullText = "";
            this.cmbKD.Properties.Columns.Add(new LookUpColumnInfo("DicNO", "省份代码"));
            this.cmbKD.Properties.Columns.Add(new LookUpColumnInfo("DicName", "省份名称"));
            this.cmbKD.Properties.DataSource = service.GetDicValueByTableName("TB_Dic_ExamProvince");
            this.cmbKD.Properties.DisplayMember = "DicName";
            this.cmbKD.Properties.ValueMember = "DicNO";
            this.cmbKD.Properties.Columns[0].Visible = false;

            using (DataTable dtExamProvince = service.GetDicValueByTableName("TB_Dic_ExamProvince"))
            {
                this.chkListExamProvince.DisplayMember = "DicName";
                this.chkListExamProvince.ValueMember = "DicNO";
                this.chkListExamProvince.DataSource = dtExamProvince;
            }
        }
        #endregion

        private void buttonX2_Click(object sender, EventArgs e)
        {
            if (cmbKD.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请选择现考点！");
                cmbKD.Focus();
                return;
            }
            if (txtNo.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请输入考点编号！");
                txtNo.Focus();
                return;
            }
            if (txtName.Text.Length == 0)
            {
                DevHelper.ShowInfoMessageBox("请输入考点名称！");
                txtName.Focus();
                return;
            }
            if (model == null)
            {
                model = new TB_Sys_ExamPlace();
            }
            else
            {
                model.Attach();
            }
            model.ExamPlaceNO = txtNo.Text;
            model.Address = txtAddress.Text;
            model.ExamPlaceName = txtName.Text;
            model.ExamProvince = cmbKD.EditValue.ToSafeString();
            model.ExamYear = SessionerHelper.SystemYear;
            if (chkIsClose.Checked)
            {
                model.IsActive = "1";
            }
            else
            {
                model.IsActive = "0";
            }
            if (service.SaveSysExamPlace(model))
            {
                List<TB_Sys_ExamPlace_Limit> list = new List<TB_Sys_ExamPlace_Limit>();
                if (chkListExamProvince.CheckedItems.Count > 0)
                {
                    for (int i = 0; i < chkListExamProvince.CheckedItems.Count; i++)
                    {
                        TB_Sys_ExamPlace_Limit modelLimit = new TB_Sys_ExamPlace_Limit();
                        modelLimit.ExamPlaceNO = model.ExamPlaceNO;
                        modelLimit.ExamProvince = chkListExamProvince.CheckedItems[i].ToString();
                        list.Add(modelLimit);
                    }
                }
                else
                {
                    list.Add(new TB_Sys_ExamPlace_Limit() { ExamPlaceNO = model.ExamPlaceNO, ExamProvince = "" });
                }
                service.SaveSysExamPlaceLimit(list);
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
